package com.changgou.order.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.OrderService;
import com.changgou.order.pojo.Order;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Order> orderList = orderService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", orderList);
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        Order order = orderService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", order);
    }


    /***
     * 新增数据
     * @param order
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Order order) {
        //获取登录人的名称
        String userName = tokenDecode.getUserInfo().get("user_name");
        order.setUsername(userName);
        String orderId = orderService.add(order);
        System.out.println(orderId);
        return new Result(true, StatusCode.OK, "添加成功", orderId);
    }


    /***
     * 修改数据
     * @param order
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Order order, @PathVariable String id) {
        order.setId(id);
        orderService.update(order);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") String id) {
        orderService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        List<Order> list = orderService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }


    /***
     * 分页搜索实现
     * @param
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody Map searchMap, @PathVariable("page")  int page, @PathVariable("size")  int size){
        Page<Order> pageList = orderService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
    //发货
    @PostMapping("/batchSend")
    public Result batchSend(@RequestBody List<Order> orders) {
        orderService.batchSend(orders);
        return new Result(true, StatusCode.OK, "发货成功");
    }

    /**
     * 根据用户查询订单列表
     */
    @GetMapping("/list")
    public Result<List<Order>> list() {
        String username = tokenDecode.getUserInfo().get("user_name");
        List<Order> orderList = orderService.list(username);
        return new Result<>(true, StatusCode.OK, "查询成功", orderList);
    }

    /**
     * 取消订单
     */
    @RequestMapping("/closeOrder")
    public Result closeOrder(@RequestParam("orderId") String orderId) {
        orderService.closeOrder(orderId);
        return new Result(true, StatusCode.OK, "取消订单成功");
    }

    /**
     * 确认收货
     */
    @RequestMapping("/receipt")
    public Result receipt(@RequestParam("orderId") String orderId) {
        orderService.receipt(orderId);
        return new Result(true, StatusCode.OK, "确认收货成功");
    }

    //查询订单状态
    @GetMapping("/orderStatus")
    public Result findOrder(){
        List<Map<String, String>> list=orderService.findCountByOrderStatus();
        return new Result<>(true,StatusCode.OK,"查询成功",list);
    }

    @GetMapping("/profitReport")
    public Result profitReport() {
        Map<String, Object> map = new HashMap<>();
        List<String> months = new ArrayList<>();
        //获得日历对象
        Calendar calendar = Calendar.getInstance();
        //获得当前时间往前推12个月
        calendar.add(Calendar.MONTH, -12);
        //计算过去一年的12个月 分别的月份
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
        }
        map.put("months", months);

        List<Integer> profitCount = orderService.findMemberCountByMonths(months);
        map.put("userCount", profitCount);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }
    /**
     * 查询未付款订单
     */
    @GetMapping("/findList2Pay")
    public Result<List<Order>> findList2Pay(){
        String username = tokenDecode.getUserInfo().get("user_name");
        List<Order> orderList = orderService.findList2Pay(username);
        return new Result<>(true, StatusCode.OK, "查询成功", orderList);
    }

    /**
     * 查询未付款订单
     */
    @GetMapping("/findList2Send")
    public Result<List<Order>> findList2Send(){
        String username = tokenDecode.getUserInfo().get("user_name");
        List<Order> orderList = orderService.findList2Send(username);
        return new Result<>(true, StatusCode.OK, "查询成功", orderList);
    }

    /**
     * 查询未收货订单
     */
    @GetMapping("/findList2Receive")
    public Result<List<Order>> findList2Receive(){
        String username = tokenDecode.getUserInfo().get("user_name");
        List<Order> orderList = orderService.findList2Receive(username);
        return new Result<>(true, StatusCode.OK, "查询成功", orderList);
    }

    /**
     * 查询未评价订单
     */
    @GetMapping("/findList2Evaluation")
    public Result<List<Order>> findList2Evaluation(){
        String username = tokenDecode.getUserInfo().get("user_name");
        List<Order> orderList = orderService.findList2Evaluation(username);
        return new Result<>(true, StatusCode.OK, "查询成功", orderList);
    }

    /**
     * 评价订单
     */
    @GetMapping("/evaluation/{orderId}")
    public Result evaluation(@PathVariable("orderId")String orderId,@RequestParam("buyerRate") String buyerRate ,@RequestParam("evaluation") String evaluation){
        orderService.evaluation(orderId,buyerRate,evaluation);
        return new Result<>(true, StatusCode.OK, "评价成功");

    }
}
